<script setup lang="ts">
  import { ref, watch } from 'vue'
  import ProjectItem from './project-item.vue'
  const props = defineProps({
    pageType: {
      type: String,
      default: '',
    },
    record: {
      type: Object,
      default: () => {},
    },
  })

  const details = ref<any>({})

  const deleteItem = (repairItemId: number | string) => {
    console.log('deleteItem', details.value.detailList)

    details.value.detailList.forEach(function (item: any, index: number) {
      if (item.repairItemId === repairItemId) {
        details.value.detailList.splice(index, 1)
      }
    })
    // 重新计算(完成保养)
    getAllCounts()
  }

  const repairAmount = ref(0)
  const getAllCounts = () => {
    repairAmount.value = 0
    details.value.detailList.forEach((i: any) => {
      repairAmount.value += i.price ? i.price : 0
    })
    details.value.repairAmount = repairAmount.value
  }

  // project-item页面删除一项
  // uni.$on('projectItemdelete', function (repairItemId) {
  //   deleteItem(repairItemId)
  //   getAllCounts()
  // })

  watch(
    () => props.record,
    (val) => {
      details.value = val
      getAllCounts()
    },
    { immediate: true, deep: true },
  )

  defineExpose({
    deleteItem,
    getAllCounts,
  })
</script>

<template>
  <!-- <view
    class="projects c1 fs30 fw400"
    v-if="(record.status == 0 || pageType == 'create' || pageType == '' || pageType == 'start') && record.detailList"
  >
    <view
      class="pro"
      v-for="(item, index) in record.detailList"
      :key="index"
      :style="index + 1 == record.detailList.length ? 'margin-bottom: 32rpx' : ''"
    >
      <view class="c1 fs30 fw400">{{ item.repairItem }}</view>
      <view class="pro-delete" @click.stop="deleteItem(item.repairItemId)">
        <ve-icon name="delete" size="28" style="margin-right: 8rpx" />
        <view class="c5 fs30 fw400">删除</view>
      </view>
    </view>
  </view> -->
  <view class="projects-end" v-if="details.detailList && details.detailList.length">
    <ProjectItem
      :proType="props.pageType"
      v-for="(item, index) in details.detailList"
      :key="index"
      :proItem="item"
      :proIndex="index"
      @getAllCount="getAllCounts"
      @delete="deleteItem"
    />
    <view class="line"></view>
    <ve-cell-price label="合计" :value="details.repairAmount" priceAlign="right"></ve-cell-price>
  </view>
</template>

<style lang="scss" scoped>
  .projects {
    width: 100%;
    padding: 0 24rpx;
    background: #f7f8fa;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
    .pro {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      margin-top: 32rpx;
      .pro-delete {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
  .projects-end {
    width: 100%;
    background: #f7f8fa;
    padding: 0 24rpx 2rpx;
    .line {
      width: 100%;
      height: 2rpx;
      background: rgba(216, 216, 216, 0.3);
    }
  }
</style>
